\hypertarget{group__nxbot__motors}{
\section{MOTORS Library}
\label{group__nxbot__motors}\index{MOTORS Library@{MOTORS Library}}
}


Motor library for controlling the velocities and for reading the odometers of the two motors of NXBot.  


\subsection*{Defines}
\begin{DoxyCompactItemize}
\item 
\#define \hyperlink{group__nxbot__motors_gaa5226d999132effe3c883ffd4e974f47}{TICS\_\-PER\_\-ROT}~362
\item 
\#define \hyperlink{group__nxbot__motors_gaaa6f04f6f0d7cf1008a53279acf49f07}{WHEEL\_\-RADIUS}~33
\item 
\#define \hyperlink{group__nxbot__motors_ga598a3330b3c21701223ee0ca14316eca}{PI}~3.14159
\item 
\#define \hyperlink{group__nxbot__motors_ga85ae9767b23edf40871541d23962784b}{DIR\_\-RIGHT}~4
\item 
\#define \hyperlink{group__nxbot__motors_ga788d3497514ea05602fd974d7bdcdbde}{DIR\_\-LEFT}~3
\item 
\#define \hyperlink{group__nxbot__motors_ga9eff471d17bc68f18b14ff75f23aa3bc}{MOTOR\_\-RIGHT}~1
\item 
\#define \hyperlink{group__nxbot__motors_ga0e2d750df9008232cafa398d2424b778}{MOTOR\_\-LEFT}~0
\item 
\#define \hyperlink{group__nxbot__motors_ga249e00fdccfe7edf6ebbcf22a17af4ed}{MAX\_\-VEL}~100
\item 
\#define \hyperlink{group__nxbot__motors_ga6d7874fc65ae0da8c12558b134a0a480}{VEL\_\-FACT}~320
\begin{DoxyCompactList}\small\item\em Relationship between the sample frequency and the max velocity. VEL\_\-FACT =. \item\end{DoxyCompactList}\item 
\#define \hyperlink{group__nxbot__motors_gab79073453a999b8854031f2af0df0b3c}{motors\_\-getActualLPos}()~encoders\_\-readLeft();
\item 
\#define \hyperlink{group__nxbot__motors_gadded0d8d62068adf34807efd68a3f6cd}{motors\_\-getActualRPos}()~encoders\_\-readRight();
\item 
\#define \hyperlink{group__nxbot__motors_ga62ff321b95fa46fbeb91e71557dd9961}{motors\_\-setDirection}(l, r)~encoders\_\-setDirection(l,r);
\item 
\#define \hyperlink{group__nxbot__motors_ga2d72f539284abc124dbc2acc79199f6d}{motors\_\-getDirection}(l, r)~encoders\_\-getDirection(l,r);
\end{DoxyCompactItemize}
\subsection*{Functions}
\begin{DoxyCompactItemize}
\item 
void \hyperlink{group__nxbot__motors_gacfe62534ff1455ef55a6d71561428051}{motors\_\-getActualVels} (\hyperlink{structmotors__t}{motors\_\-t} $\ast$motors, unsigned int sampleFreq)
\begin{DoxyCompactList}\small\item\em Updates the {\ttfamily \#velocities} of {\ttfamily \#motors} structure with the actual velocities. \item\end{DoxyCompactList}\item 
void \hyperlink{group__nxbot__motors_ga1669ce1fedb2a775eaac797e0f6a6324}{motors\_\-init} (unsigned char freq)
\begin{DoxyCompactList}\small\item\em Initializes all the hardware used for the motors (encoders and pwm). \item\end{DoxyCompactList}\item 
void \hyperlink{group__nxbot__motors_gacb966263b9e0b54a8c75e2b4db16afc4}{motors\_\-setVel} (int vel, unsigned char motor)
\begin{DoxyCompactList}\small\item\em Sets the desired velocity for each motor. \item\end{DoxyCompactList}\item 
void \hyperlink{group__nxbot__motors_ga1b137285625f6a9b621a3c1ba0733201}{motors\_\-getActualPos} (\hyperlink{structmotors__t}{motors\_\-t} $\ast$motors)
\begin{DoxyCompactList}\small\item\em Updates the {\ttfamily \#odometers} of {\ttfamily \#motors} structure with the actual encoders reading. \item\end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Detailed Description}
Motor library for controlling the velocities and for reading the odometers of the two motors of NXBot. 
\begin{DoxyCode}
 #include <motors.h> 
\end{DoxyCode}
 This module uses timer0 of the ATmega1281 microcontroller for generating the desired PWM signals. Timer0 is configured in {\itshape Free Running mode\/}, and it is constantly compared with two registers (OC0 and OC1), in order to toggle the logical state of two pins, where the PWM signals to the motors are connected. Another two signals are used in order to set the desired direction of the motor. For FORWARD direction use {\bfseries positive} values, for BACKWARD direction use {\bfseries negative} values. This library does not need to have the interrupts in order to function correctly, it is not an interrupt-\/driven module. 

\subsection{Define Documentation}
\hypertarget{group__nxbot__motors_ga788d3497514ea05602fd974d7bdcdbde}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!DIR\_\-LEFT@{DIR\_\-LEFT}}
\index{DIR\_\-LEFT@{DIR\_\-LEFT}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{DIR\_\-LEFT}]{\setlength{\rightskip}{0pt plus 5cm}\#define DIR\_\-LEFT~3}}
\label{group__nxbot__motors_ga788d3497514ea05602fd974d7bdcdbde}
\hypertarget{group__nxbot__motors_ga85ae9767b23edf40871541d23962784b}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!DIR\_\-RIGHT@{DIR\_\-RIGHT}}
\index{DIR\_\-RIGHT@{DIR\_\-RIGHT}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{DIR\_\-RIGHT}]{\setlength{\rightskip}{0pt plus 5cm}\#define DIR\_\-RIGHT~4}}
\label{group__nxbot__motors_ga85ae9767b23edf40871541d23962784b}
\hypertarget{group__nxbot__motors_ga249e00fdccfe7edf6ebbcf22a17af4ed}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!MAX\_\-VEL@{MAX\_\-VEL}}
\index{MAX\_\-VEL@{MAX\_\-VEL}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{MAX\_\-VEL}]{\setlength{\rightskip}{0pt plus 5cm}\#define MAX\_\-VEL~100}}
\label{group__nxbot__motors_ga249e00fdccfe7edf6ebbcf22a17af4ed}
\hypertarget{group__nxbot__motors_ga0e2d750df9008232cafa398d2424b778}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!MOTOR\_\-LEFT@{MOTOR\_\-LEFT}}
\index{MOTOR\_\-LEFT@{MOTOR\_\-LEFT}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{MOTOR\_\-LEFT}]{\setlength{\rightskip}{0pt plus 5cm}\#define MOTOR\_\-LEFT~0}}
\label{group__nxbot__motors_ga0e2d750df9008232cafa398d2424b778}
\hypertarget{group__nxbot__motors_ga9eff471d17bc68f18b14ff75f23aa3bc}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!MOTOR\_\-RIGHT@{MOTOR\_\-RIGHT}}
\index{MOTOR\_\-RIGHT@{MOTOR\_\-RIGHT}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{MOTOR\_\-RIGHT}]{\setlength{\rightskip}{0pt plus 5cm}\#define MOTOR\_\-RIGHT~1}}
\label{group__nxbot__motors_ga9eff471d17bc68f18b14ff75f23aa3bc}
\hypertarget{group__nxbot__motors_gab79073453a999b8854031f2af0df0b3c}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!motors\_\-getActualLPos@{motors\_\-getActualLPos}}
\index{motors\_\-getActualLPos@{motors\_\-getActualLPos}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{motors\_\-getActualLPos}]{\setlength{\rightskip}{0pt plus 5cm}\#define motors\_\-getActualLPos(
\begin{DoxyParamCaption}
{}
\end{DoxyParamCaption}
)~encoders\_\-readLeft();}}
\label{group__nxbot__motors_gab79073453a999b8854031f2af0df0b3c}
\hypertarget{group__nxbot__motors_gadded0d8d62068adf34807efd68a3f6cd}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!motors\_\-getActualRPos@{motors\_\-getActualRPos}}
\index{motors\_\-getActualRPos@{motors\_\-getActualRPos}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{motors\_\-getActualRPos}]{\setlength{\rightskip}{0pt plus 5cm}\#define motors\_\-getActualRPos(
\begin{DoxyParamCaption}
{}
\end{DoxyParamCaption}
)~encoders\_\-readRight();}}
\label{group__nxbot__motors_gadded0d8d62068adf34807efd68a3f6cd}
\hypertarget{group__nxbot__motors_ga2d72f539284abc124dbc2acc79199f6d}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!motors\_\-getDirection@{motors\_\-getDirection}}
\index{motors\_\-getDirection@{motors\_\-getDirection}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{motors\_\-getDirection}]{\setlength{\rightskip}{0pt plus 5cm}\#define motors\_\-getDirection(
\begin{DoxyParamCaption}
\item[{}]{l, }
\item[{}]{r}
\end{DoxyParamCaption}
)~encoders\_\-getDirection(l,r);}}
\label{group__nxbot__motors_ga2d72f539284abc124dbc2acc79199f6d}
\hypertarget{group__nxbot__motors_ga62ff321b95fa46fbeb91e71557dd9961}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!motors\_\-setDirection@{motors\_\-setDirection}}
\index{motors\_\-setDirection@{motors\_\-setDirection}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{motors\_\-setDirection}]{\setlength{\rightskip}{0pt plus 5cm}\#define motors\_\-setDirection(
\begin{DoxyParamCaption}
\item[{}]{l, }
\item[{}]{r}
\end{DoxyParamCaption}
)~encoders\_\-setDirection(l,r);}}
\label{group__nxbot__motors_ga62ff321b95fa46fbeb91e71557dd9961}
\hypertarget{group__nxbot__motors_ga598a3330b3c21701223ee0ca14316eca}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!PI@{PI}}
\index{PI@{PI}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{PI}]{\setlength{\rightskip}{0pt plus 5cm}\#define PI~3.14159}}
\label{group__nxbot__motors_ga598a3330b3c21701223ee0ca14316eca}
\hypertarget{group__nxbot__motors_gaa5226d999132effe3c883ffd4e974f47}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!TICS\_\-PER\_\-ROT@{TICS\_\-PER\_\-ROT}}
\index{TICS\_\-PER\_\-ROT@{TICS\_\-PER\_\-ROT}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{TICS\_\-PER\_\-ROT}]{\setlength{\rightskip}{0pt plus 5cm}\#define TICS\_\-PER\_\-ROT~362}}
\label{group__nxbot__motors_gaa5226d999132effe3c883ffd4e974f47}
\hypertarget{group__nxbot__motors_ga6d7874fc65ae0da8c12558b134a0a480}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!VEL\_\-FACT@{VEL\_\-FACT}}
\index{VEL\_\-FACT@{VEL\_\-FACT}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{VEL\_\-FACT}]{\setlength{\rightskip}{0pt plus 5cm}\#define VEL\_\-FACT~320}}
\label{group__nxbot__motors_ga6d7874fc65ae0da8c12558b134a0a480}


Relationship between the sample frequency and the max velocity. VEL\_\-FACT =. 

\hypertarget{group__nxbot__motors_gaaa6f04f6f0d7cf1008a53279acf49f07}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!WHEEL\_\-RADIUS@{WHEEL\_\-RADIUS}}
\index{WHEEL\_\-RADIUS@{WHEEL\_\-RADIUS}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{WHEEL\_\-RADIUS}]{\setlength{\rightskip}{0pt plus 5cm}\#define WHEEL\_\-RADIUS~33}}
\label{group__nxbot__motors_gaaa6f04f6f0d7cf1008a53279acf49f07}


\subsection{Function Documentation}
\hypertarget{group__nxbot__motors_ga1b137285625f6a9b621a3c1ba0733201}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!motors\_\-getActualPos@{motors\_\-getActualPos}}
\index{motors\_\-getActualPos@{motors\_\-getActualPos}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{motors\_\-getActualPos}]{\setlength{\rightskip}{0pt plus 5cm}void motors\_\-getActualPos (
\begin{DoxyParamCaption}
\item[{{\bf motors\_\-t} $\ast$}]{motors}
\end{DoxyParamCaption}
)}}
\label{group__nxbot__motors_ga1b137285625f6a9b621a3c1ba0733201}


Updates the {\ttfamily \#odometers} of {\ttfamily \#motors} structure with the actual encoders reading. 

The \#position is measured in pulses.


\begin{DoxyParams}{Parameters}
{\em motors} & The pointer to the {\ttfamily \#motors} structure. \\
\hline
\end{DoxyParams}
\hypertarget{group__nxbot__motors_gacfe62534ff1455ef55a6d71561428051}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!motors\_\-getActualVels@{motors\_\-getActualVels}}
\index{motors\_\-getActualVels@{motors\_\-getActualVels}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{motors\_\-getActualVels}]{\setlength{\rightskip}{0pt plus 5cm}void motors\_\-getActualVels (
\begin{DoxyParamCaption}
\item[{{\bf motors\_\-t} $\ast$}]{motors, }
\item[{unsigned int}]{sampleFreq}
\end{DoxyParamCaption}
)}}
\label{group__nxbot__motors_gacfe62534ff1455ef55a6d71561428051}


Updates the {\ttfamily \#velocities} of {\ttfamily \#motors} structure with the actual velocities. 

The \#velocities are measured in \% units in respect to the max velocity. The max velocity is dependant on the actual voltage of the battery. The measuring follows the function: \[ vel[%] = sqrt{1} \] 
\begin{DoxyParams}{Parameters}
{\em vels} & The pointer to the {\ttfamily \#velocities} structure. \\
\hline
{\em sampleFreq} & The frequency of the call to this function, in Hz. \\
\hline
\end{DoxyParams}
\hypertarget{group__nxbot__motors_ga1669ce1fedb2a775eaac797e0f6a6324}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!motors\_\-init@{motors\_\-init}}
\index{motors\_\-init@{motors\_\-init}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{motors\_\-init}]{\setlength{\rightskip}{0pt plus 5cm}void motors\_\-init (
\begin{DoxyParamCaption}
\item[{unsigned char}]{freq}
\end{DoxyParamCaption}
)}}
\label{group__nxbot__motors_ga1669ce1fedb2a775eaac797e0f6a6324}


Initializes all the hardware used for the motors (encoders and pwm). 

Configures the pwm frequency for the motors and starts the odometers. 
\begin{DoxyParams}{Parameters}
{\em vels} & The frequency for pwm. \\
\hline
\end{DoxyParams}
\hypertarget{group__nxbot__motors_gacb966263b9e0b54a8c75e2b4db16afc4}{
\index{nxbot\_\-motors@{nxbot\_\-motors}!motors\_\-setVel@{motors\_\-setVel}}
\index{motors\_\-setVel@{motors\_\-setVel}!nxbot_motors@{nxbot\_\-motors}}
\subsubsection[{motors\_\-setVel}]{\setlength{\rightskip}{0pt plus 5cm}void motors\_\-setVel (
\begin{DoxyParamCaption}
\item[{int}]{vel, }
\item[{unsigned char}]{motor}
\end{DoxyParamCaption}
)}}
\label{group__nxbot__motors_gacb966263b9e0b54a8c75e2b4db16afc4}


Sets the desired velocity for each motor. 

The velocity is defined as a percentage(\%) of the max. velocity, which is dependant on the voltage of the battery. 
\begin{DoxyParams}{Parameters}
{\em vel} & The desired velocity. It is an int betwen 0 and 100. Larger values are truncated to 100. If the parameter is negative, the motor will run backwards. \\
\hline
{\em motor} & The selected motor. It has to be one of the following constants: 
\begin{DoxyItemize}
\item MOTOR\_\-LEFT 
\item MOTOR\_\-RIGHT 
\end{DoxyItemize}\\
\hline
\end{DoxyParams}
